﻿/*
 *  Copyright (c) 2012 Brandon Ellenberger
 *
 *  This file is part of OpSatPlan.
 *
 *  OpSatPlan is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpSatPlan is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpSatPlan.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OpSatPlan
{
    public class AtomicFormula
    {
        private PredicateDefinition mDefinition;
        private ReadOnlyCollection<PddlObject> mArguments;
		private int? mHashCode;
        protected AtomicFormula()
        {
        }
        public AtomicFormula(PredicateDefinition def, IList<PddlObject> args)
        {
            Assertion.Assert<ArgumentNullException>(def != null, "Atomic Formula Definition cannot be null");
            Assertion.Assert<ArgumentNullException>(args != null, "Object arguments cannot be null");
            this.mDefinition = def;
            this.mArguments = new ReadOnlyCollection<PddlObject>(args);
			this.mHashCode = null;
        }
        public virtual PredicateDefinition Definition
        {
            get
            {
                return this.mDefinition;
            }
        }
        public virtual ReadOnlyCollection<PddlObject> Arguments
        {
            get
            {
                return this.mArguments;
            }
        }
        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            else if (obj.GetType() == typeof(AtomicFormula))
                return this == (AtomicFormula)obj;
            else
                return false;
        }

        public static bool operator ==(AtomicFormula x, AtomicFormula y)
        {
            if (System.Object.ReferenceEquals(x, y))
                return true;
            else if ((object)x == null || (object)y == null)
                return false;
            else
            {
                if (x.Arguments.Count != y.Arguments.Count)
                    return false;
                return x.GetHashCode() == y.GetHashCode();
            }
                
        }

        public static bool operator !=(AtomicFormula x, AtomicFormula y)
        {
            return !(x == y);
        }

        public override int GetHashCode()
        {
			if(this.mHashCode == null)
			{
	            int result = 17;
	            result = result * 37 + this.mDefinition.GetHashCode();
	            for (int i = 0; i < this.mArguments.Count; i++)
	                result = result * 37 + this.mArguments[i].GetHashCode();
				this.mHashCode = result;
			}
            return this.mHashCode.Value;
        }
        public override string ToString()
        {
            string text =  "(" + this.mDefinition.Name + " ";
            foreach (PddlObject obj in this.mArguments)
                text += obj.Name + " ";
            return text + ")";
        }
    }
}
